angular.module('nonghua.services', [])
    .factory('cordovaCamera', ['$q', function ($q) {
        // modified from $cordovaCamera
        return {
            getPicture: function (options) {
                var q = $q.defer();

                if (!navigator.camera) {
                    q.resolve(null);
                    return q.promise;
                }

                navigator.camera.getPicture(function (imageData) {
                    q.resolve(imageData);
                }, function (err) {
                    q.reject(err);
                }, options);

                return q.promise;
            },

            cleanup: function () {
                var q = $q.defer();

                navigator.camera.cleanup(function () {
                    q.resolve();
                }, function (err) {
                    q.reject(err);
                });

                return q.promise;
            }
        };
    }])
    .factory('Role', function ($rootScope) {
        var getRole = function(roleName){
            var role = $rootScope.user.roles;
            for(p in role){
                if(role[p] === roleName){
                    return true;
                }
            }
            return false;
        };
        return {
            isRole: function (roleName) {
               return getRole(roleName);
            }
        };
    })
    .factory('UserInfo', function ($rootScope,$q) {
        function _loadUserInfo(uid){
            var deferred = $q.defer();
            var promise = deferred.promise;

            user_load(uid,{
                success:function(account) {
                    console.log(account);
                    $rootScope.user.roles = account.roles;
                    if(!empty(account.picture) && !empty(account.picture.url)){
                        $rootScope.user.picture = account.picture.url;
                    }else{
                        $rootScope.user.picture=$rootScope.picture_default;
                    }
                    if(!empty(account.farmer_name)){
                        rootScope.user.farmer_name= account.farmer_name;
                    }
                    if(!empty(account.field_cell)){
                        $rootScope.user.cell = account.field_cell.und[0].value;
                    }

                    deferred.resolve($rootScope.user);
                }
            });
            return promise;
        }

        return {
            refreshUserInfo: function (uid) {
                return _loadUserInfo(uid);
            },
        };
    })
    .factory('Sms', function ($ionicLoading,$q) {
        function _checkAndLogin(data){
            var deferred = $q.defer();
            var promise = deferred.promise;
            var path_to = "sms/login";
            var return_data = {};

            var param = {
                checkDigit:data.checkDigit,
                userCell:data.userCell
            };
            _rest_service_resources({
                param:param,
                path: path_to,
                method: 'POST',
                success: function (result) {
                    if(result.sms_check == 0){
                        return_data.checkDigitBack = false;
                        switch (result.error){
                            case 1:
                                return_data.error = "校验码超时";
                                break;
                            case 2:
                                return_data.error = "校验码错误";
                                break;
                            case 3:
                                return_data.error = "校验码为空";
                                break;
                            case 4:
                                return_data.error = "该手机号没有绑定用户";
                                break;
                        }

                    }else if(result.sms_check == 1){
                        return_data.checkDigitBack = true;
                        return_data.user = result.user;
                    }
                    deferred.resolve(return_data);
                }
            });
            return promise;
        }
        function _getCheckResult(data){
            var deferred = $q.defer();
            var promise = deferred.promise;
            var path_to = "sms/check";
            var return_data = {};
            var param = {
                checkDigit:data.checkDigit
            };
            _rest_service_resources({
                param:param,
                path: path_to,
                method: 'POST',
                success: function (result) {
                    if(result.sms_check == 0){
                        return_data.checkDigitBack = false;
                        switch (result.error){
                            case 1:
                                return_data.error = "校验码超时";
                                break;
                            case 2:
                                return_data.error = "校验码错误";
                                break;
                            case 3:
                                return_data.error = "校验码为空";
                                break;
                        }
                    }else if(result.sms_check == 1){
                        return_data.checkDigitBack = true;
                    }
                    deferred.resolve(return_data);
                }
            });
            return promise;
        }
        function _getCheckResultRegister(data){
            var deferred = $q.defer();
            var promise = deferred.promise;
            var path_to = "sms/registerCheck";
            var return_data = {};
            var param = {
                checkDigit:data.checkDigit,
                field_cell:data.field_cell,
                name:data.name
            };
            _rest_service_resources({
                param:param,
                path: path_to,
                method: 'POST',
                success: function (result) {
                    if(result.sms_check == 0){
                        return_data.checkDigitBack = false;
                        switch (result.error){
                            case 1:
                                return_data.error = "校验码超时";
                                break;
                            case 2:
                                return_data.error = "校验码错误";
                                break;
                            case 3:
                                return_data.error = "校验码 手机号 用户名 为空";
                                break;
                            case 4:
                                return_data.error = "用户名已存在";
                                break;
                            case 5:
                                return_data.error = "手机号已存在";
                                break;
                        }
                    }else if(result.sms_check == 1){
                        return_data.checkDigitBack = true;
                    }
                    deferred.resolve(return_data);
                }
            });
            return promise;
        }
        return {
            getCheckResult: function (data) {
                return _getCheckResult(data);
            },
            getCheckResultRegister: function (data) {
                return _getCheckResultRegister(data);
            },
            checkAndLogin: function (data) {
                return _checkAndLogin(data);
            }
        };
    });